<template>
  <div>
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          label-width="68px"
          @submit.native.prevent
        >
          <el-form-item label="批次名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="模糊查询"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="selectBatch"
            />
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="selectBatch"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="clickAdd"
              >新增</el-button
            >
          </el-col>
        </el-row>
        <el-table v-loading="loading" :data="batchList">
          <!-- <el-table-column type="selection" width="50" align="center" /> -->
          <el-table-column label="批次id" align="center" key="id" prop="id" />
          <el-table-column
            label="批次名称"
            align="center"
            key="name"
            prop="name"
          />
          <el-table-column
            label="批次编号"
            align="center"
            key="code"
            prop="code"
          />
          <el-table-column
            label="开始时间"
            align="center"
            key="startDate"
            prop="startDate"
          />
          <el-table-column
            label="结束时间"
            align="center"
            key="endDate"
            prop="endDate"
          />
          <el-table-column label="状态" align="center" min-width="120">
        <template slot-scope="scope">
          <span key="status" prop="status" v-if="scope.row.status == 0">未开始</span>
          <span key="status" prop="status" v-if="scope.row.status == 1">进行中</span>
          <span key="status" prop="status" v-if="scope.row.status == 3">已结束</span>
        </template>
      </el-table-column>
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            v-if="columns[3].visible"
            width="160"
          >
          </el-table-column>
          <el-table-column
            label="修改批次状态"
            align="center"
            width="380"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button type="success" v-if="scope.row.status==0" round plain size="mini" @click="updateStatus1(scope.row)">进行中</el-button>
              <el-button type="warning" v-if="scope.row.status==1" round plain size="mini" @click="updateStatus3(scope.row)">已结束</el-button>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="250"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
              <el-button type="text" icon="el-icon-plus" @click="selectCourse(scope.row)">选择</el-button>

              <el-dialog title="选择课程" :visible.sync="dialogTableVisible">
                <el-table ref="multipleTable" :data="courseList">
                  <el-table-column type="selection" width="50" align="center" />
                  <el-table-column property="id" v-if="false" label="课程ID"></el-table-column>
                  <el-table-column property="batchId" v-if="false">{{batchId}}</el-table-column>
                  <el-table-column property="name" label="课程名称"></el-table-column>
                  <el-table-column property="industryName" label="行业"></el-table-column>
                  <el-table-column property="typeName" label="类型"></el-table-column>
                  <el-table-column property="classHours" label="学时"></el-table-column>
                  <el-table-column property="createTime" label="开设时间"></el-table-column>
                </el-table>
                <div>&nbsp;</div>
                <el-button type="primary" round @click="insert(scope.row)">确认提交</el-button>
              </el-dialog>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <pagination
          v-show="this.sum > 0"
          :total="this.sum"
          :page.sync="queryParams.page"
          :limit.sync="queryParams.number"
          @pagination="selectBatch"
        />   
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="批次名称" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入批次名称"
                maxlength="40"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="批次编号">
              <el-input
                v-model="form.code"
                placeholder="请输入批次编号"
                maxlength="40"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="选择日期">
              <el-date-picker
                v-model="form.startDate"
                type="date"
                placeholder="开始日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item>
              <el-date-picker
                v-model="form.endDate"
                type="date"
                placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  selectBatchApi,
  deleteBatchApi,
  insertBatchApi,
  updateBatchApi,
  updataStatusApi,
  selectCourseInfoApi,
  insertApi
} from "@/api/system/batch";
export default {
  name: "batch",
  // components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      loadingBatch: true,
      refreshTable: true,
      // 导出遮罩层
      exportLoading: false,
      // 学院表格数据
      batchList: null,
      courseList: null,
      sum: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据
      ids: [],
      batchId: null,
      form: {},
      //   collegeId: undefined,
      // 查询参数
      queryParams: {
        page: 1,
        number: 10,
        name: undefined,
      },
      // // 列信息
      columns: [
        { key: 0, label: `批次编号`, visible: true },
        { key: 1, label: `批次名称`, visible: true },
        { key: 2, label: `状态`, visible: true },
        { key: 3, label: `创建时间`, visible: true },
      ],
      // 表单校验
      rules: {},
      dialogTableVisible: false
    };
  },
  watch: {},
  created() {
    this.selectBatch();
  },
  methods: {
    //查询批次
    selectBatch() {
      selectBatchApi(this.queryParams)
        .then((data) => {
          this.batchList = data.data.returnDate;
          this.sum = data.data.paginationData.sum;
          this.loading = false;
        })
        .catch(() => {
          this.batchList = null;
          this.sum = 0;
          this.loading = false;
        });
    },
    //查询授课
    selectCourse(row) {
      this.dialogTableVisible = true;
      selectCourseInfoApi({
        page: 1,
        number: 10
      })
        .then((data) => {
          this.batchId = row.id;
          this.courseList = data.data.returnDate;
          this.sum = data.data.paginationData.sum;
          this.loading = false;
        })
        .catch(() => {
          this.courseList = null;
          this.sum = 0;
          this.loading = false;
        });
    },
    insert(){
      this.$refs.multipleTable.selection.forEach(element => {
        this.ids.push(element.id)
      })
      insertApi({
        id: this.batchId,
        ids: this.ids
      }).then((data) => {
          if(data.code==200){
                this.$message({
                  type: "success",
                  message: "操作成功"
                });
          };
          this.dialogTableVisible = false;
        })
        .catch(() => {
          this.batchId = null;
          this.ids = [];
        });
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        code:undefined,
        startDate:undefined,
        endDate:undefined,
        status:undefined
      };
       if (this.$refs["form"]) {
		    this.$refs["form"].resetFields();
	    }
    },
    /** 重置按钮操作 */
    resetQuery() {
      if (this.$refs["queryForm"]) {
        this.$refs["queryForm"].resetFields();
      }
      this.selectBatch();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除批次id为"' + row.id + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteBatchApi({
          id: row.id,
        }).then((data) => {
          this.selectBatch();
          this.$message({
            type: "success",
            message: "删除成功",
          });
        });
      });
    },
    /** 修改批次状态 */
    updateStatus0(row) {
      this.$confirm('是否确认该批次状态为未开始?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        updataStatusApi({
          id: row.id,
          status: 0
        }).then((data) => {
          this.selectBatch();
          this.$message({
            type: "success",
            message: "修改成功",
          });
        });
      });
    },
    updateStatus1(row) {
      this.$confirm('是否确认该批次状态为进行中?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        updataStatusApi({
          id: row.id,
          status: 1
        }).then((data) => {
          this.selectBatch();
          this.$message({
            type: "success",
            message: "修改成功",
          });
        });
      });
    },
    updateStatus2(row) {
      this.$confirm('是否确认该批次状态为暂停中?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        updataStatusApi({
          id: row.id,
          status: 2
        }).then((data) => {
          this.selectBatch();
          this.$message({
            type: "success",
            message: "修改成功",
          });
        });
      });
    },
    updateStatus3(row) {
      this.$confirm('是否确认该批次状态为已结束?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        updataStatusApi({
          id: row.id,
          status: 3
        }).then((data) => {
          this.selectBatch();
          this.$message({
            type: "success",
            message: "修改成功",
          });
        });
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.title = "修改批次";
      this.reset();
      selectBatchApi({
        id: row.id,
        page: 1,
        number: 1,
      }).then(() => {
        this.form.id = row.id;
        this.form.name = row.name;
        this.form.code = row.code;
        this.form.startDate = row.startDate;
        this.form.endDate = row.endDate;
        this.form.status = row.status;
        this.open = true;
        this.selectBatch();
      });
    },
    clickAdd() {
      this.title = "新增批次";
      this.open = true;
      this.reset();
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateBatchApi(this.form).then((data) => {
              this.open = false;
              this.selectBatch();
              this.reset();
              if(data.code==200){
                this.$message({
                  type: "success",
                  message: "修改成功"
              });
              }
            });
          } else {
            insertBatchApi(this.form).then((data) => {
              this.open = false;
              this.selectBatch();
              this.reset();
              if(data.code==200){
                this.$message({
                  type: "success",
                  message: "新增成功"
              });
              }
            });
          }
        }
      });
    }, // 取消按钮
    cancel() {
      this.open = false;
    },
  },
};
</script>